package com.comtop.test

import java.sql.Date
import java.util.Map
import java.util.HashMap
import scala.collection.mutable.ArrayBuilder
import org.apache.spark.rdd.RDD
import com.comtop.db.fastar.JdbcConnection



object test {
  
  
  /**
   * 变量两个数组
   */
  def adbTest(){
    val fuck1 = Array('a', 'b', 'c', 'd')
    val fuck2 = Array('y', 'o', 'c', 'l')
    for (i <- fuck1; j <- fuck2 if i == j) println(i)
  }

  
  /**
   * 数据拉链操作
   */
  def zipTest() {
    val abcList = List('a', 'b', 'c')
    val defList = List('d', 'e', 'f')
    abcList.zip(defList).foreach(x => println(x))
    
    val xs = List(1,2,3)
    val ys = List('a','b')
    val zs = List("I","II","III","IV")
    
    val x =0
    val y='_'
    val z="_"
    
    println("xs.zipAll(ys, x, y): "+xs.zipAll(ys, x, y))
    println("xs.zipAll(zs, x, y): "+xs.zipAll(zs, x, z))
    
    
    val values = List.range(1, 5, 1)
    println("values: "+values)   //values: List(1, 2, 3, 4)
    println("zipped: "+(values, values).zipped.toMap)  //Map(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4)
    println("sum: "+(values, values).zipped.map(_ * _).sum) //30
    
    
    val series = Seq(0, 1, 1, 2, 3, 5, 8, 13)
    println("series.zipWithIndex: "+series.zipWithIndex)

    val seriesIn = Seq(0, 1, 1, 2, 3, 5, 8, 13)
    val fibonacci = seriesIn.zipWithIndex
    fibonacci.unzip
    val seriesOut = fibonacci.unzip._1 //seriesOut: Seq[Int] = List(0, 1, 1, 2, 3, 5, 8, 13)
    val numbersOut = fibonacci.unzip._2 //numbersOut: Seq[Int] = List(0, 1, 2, 3, 4, 5, 6, 7)
      println("unzip函数拆分后的结果--1："+ seriesOut)
    println("unzip函数拆分后的结果--2："+ numbersOut)
  }
  
  /**
   * List操作
   * 可以使用:::运算符或列表List.:::()方法或List.concat()方法来添加两个或多个列表
   */
  def concatListTest(){
    val fruit1 = "apples" :: ("oranges" :: ("pears" :: Nil))
    val fruit2 = "mangoes" :: ("banana" :: Nil)

    // use two or more lists with ::: operator
    var fruit = fruit1 ::: fruit2
    println("fruit1 ::: fruit2 : " + fruit)

    // use two lists with Set.:::() method
    fruit = fruit1.:::(fruit2)
    println("fruit1.:::(fruit2) : " + fruit)

    // pass two or more lists as arguments
    fruit = List.concat(fruit1, fruit2)
    println("List.concat(fruit1, fruit2) : " + fruit)
  }
  
  
  /**
   * 可以使用List.fill()方法创建，包括相同的元素如下的零个或更多个拷贝的列表
   */
  def fillListTest(){
    val fruit = List.fill(3)("apples") // Repeats apples three times.
    println("fruit : " + fruit)

    val num = List.fill(10)(2) // Repeats 2, 10 times.
    println("num : " + num)
    
    //连同List.tabulate()方法制表列表之前的列表中的所有元素以应用
    println(List.tabulate(6)(n=>n*n))
    println(List.tabulate(4,5)(_*_))
  }
  
  
  /**
   * 获取list中不同位置元素
   */
  def takeTest(){
    
    val lst = List(12,34,5,6,7,8,9)
    println("tail:"+lst.tail) //tail:List(34, 5, 6, 7, 8, 9)
    println("take:"+lst.take(3)) //take:List(12, 34, 5)
    println("take:"+lst.takeRight(3)) //take:List(7, 8, 9)
    
    //takeWhile是从第一个元素开始，取满足条件的元素，直到不满足为止
    println("takeWhile:"+lst.takeWhile(_>10)) //takeWhile:List(12, 34)
    
    println("init:"+lst.init) //init:List(12, 34, 5, 6, 7, 8)
    println("inits:"+lst.inits) //inits:non-empty iterator
    println("mkString:"+lst.mkString(",")) //mkString:12,34,5,6,7,8,9

    println("list头部添加元素：" + lst.+:(3))
    println("list尾部添加元素：" + lst.:+(3))
  }
  
  
  
  /**
   * 匹配
   */
  def matchTest(x:Int):String = x match{
    case 1=>"One"
    case 2=>"Two"
    case 3=>"Three"
    case _=>"any"
  }
  
  
  /**
   * 元祖
   */
  def tupleTest(){
    val tuple = Tuple3(1,"II",'a')    
    println(tuple)
  
   /***
   * srdd2.map(x=>(x._1,(x._2._1,(x._2._2+1).asInstanceOf[Integer],x._2._3))).union(srdd2)
   */
  
    val tt = Tuple2("ss", (1,"GEGE",3))
    println(tt._2._2)
  }
  
  
  /**
   * for嵌套遍历
   */
  def embedForTest(){
    var myArr : Array[Array[String]] = new Array[Array[String]](10)
    val a=Array("a","aa","aaa","aaaa","aaaaa")
    val b=Array("b","bb","bbb","bbbb","bbbbb")
    myArr(0)=a
    myArr(1)=b
//    myArr(0).foreach { x => println(x) }
//    myArr(1).foreach { x => println(x) }
    
    //传统写法
//    for(i<-0 until myArr.length){
//      myArr(i).foreach { x => println(x) }
//    }
    
    //常用写法
    for(anArr:Array[String] <-myArr;
        anStr:String <- anArr){
      println("嵌套数组："+anStr)
    }
    
    val matrix = Array.ofDim[Double](3, 4) //构造一个三行四列的二维数组  
    matrix(0)(0) = 56 //访问第一个元素  
    println("matrix: "+matrix(0)(0))
    
    val triangle = new Array[Array[Int]](10) //创建一个十行的数组，每一行都是一个Int型的Array  
    for (i <- 0 until (triangle.length)) {  
      triangle(i) = new Array[Int](i + 1) //将每一行实例化成(i+1)列的二维数组  
    }  
    //打印数组  
    for (i <- 0 until (triangle.length)) {  
      for (j <- 0 until (triangle(i).length)) {  
        print(triangle(i)(j) + ",")  
      }  
      println()  
    }
  }
  
  
  /**
   * RDD用法
   */
  def rddTest(rdd:RDD[String]):RDD[String]={
      val s = "Hello"
      rdd.map(x => s +","+ x)
  }
  
  
  def main(args: Array[String]): Unit = {

    //      zipTest

    //        adbTest    

    //      fillListTest

    //  println(matchTest(3))

//    val array = Array("yakcy","Bone","kylin")
//    rddTest(JdbcConnection.sc.parallelize(array, 1)).foreach { x => println(x) }
    
    var txtRdd = JdbcConnection.sc.textFile("E:/backup/data/fData.txt", 1).collect()
                               .map { x => x.split(",")(1) }
                               .take(1000)
                               .map { x => println() }
//    txtRdd.map { x => x.split(",") }.collect().take(1000).map { x => println() }


  }
}